package cn.icare.cms.impl;

import cn.icare.cms.ListUtil;
import cn.icare.cms.MemberAddressService;
import cn.icare.cms.dao.mapper.MemberAddressMapper;
import cn.icare.cms.dao.po.ItemStore;
import cn.icare.cms.dao.po.MemberAddress;
import cn.icare.cms.model.ro.AddMemberAddressRO;
import cn.icare.cms.model.ro.EditMemberAddressRO;
import cn.icare.cms.model.vo.rest.MsgDatasVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class MemberAddressServiceImpl implements MemberAddressService {

    @Resource
    private MemberAddressMapper memberAddressMapper;

    private final Logger logger = LoggerFactory.getLogger(MemberAddressServiceImpl.class);


    @Override
    public Long createOne(AddMemberAddressRO ro, Long memberId) {
        try {
            MemberAddress model = new MemberAddress();
            model.setMemberid(memberId);
            model.setName(ro.getName());
            model.setSex(ro.getSex());
            model.setGpsponit(ro.getGpsponit());
            model.setGpsponitname(ro.getGpsponitname());
            model.setMobile(ro.getMobile());
            model.setAddress(ro.getAddress());
            model.setIsdefault(ro.getIsdefault());
            model.setLat(ro.getLat());
            model.setLon(ro.getLon());
            model.setCreatetime(new Date());
            model.setAreacode(ro.getAreacode());
            if(model.getIsdefault()==1)
            {
                //将会员的其它地址设置为非默认
                this.updateIsDefault(memberId,0);
            }
            int result = this.memberAddressMapper.insertSelective(model);
            if (result > 0) {
                return model.getId();
            }
        } catch (Exception ex) {
            this.logger.error("createOne异常", ex);
        }
        return 0L;
    }

    @Override
    public boolean updateOne(EditMemberAddressRO ro,Long memberId) {
        try {
            MemberAddress model = new MemberAddress();
            model.setId(ro.getId());
            model.setName(ro.getName());
            model.setSex(ro.getSex());
            model.setGpsponit(ro.getGpsponit());
            model.setGpsponitname(ro.getGpsponitname());
            model.setMobile(ro.getMobile());
            model.setAddress(ro.getAddress());
            model.setIsdefault(ro.getIsdefault());
            model.setLat(ro.getLat());
            model.setLon(ro.getLon());
            model.setCreatetime(new Date());
            model.setAreacode(ro.getAreacode());
            Example example = new Example(MemberAddress.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("id",ro.getId());
            criteria.andEqualTo("memberid",memberId);

            if(model.getIsdefault()==1)
            {
                //将会员的其它地址设置为非默认
                this.updateIsDefault(memberId,0);
            }
            int result = this.memberAddressMapper.updateByExampleSelective(model,example);
            if (result > 0) {
                return true;
            }
        } catch (Exception ex) {
            this.logger.error("updateOne", ex);
        }
        return false;
    }

    @Override
    public boolean deleteOne(Long id,Long memberId) {
        Example example = new Example(MemberAddress.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("id",id);
        criteria.andEqualTo("memberid",memberId);
        return this.memberAddressMapper.deleteByExample(example) > 0;
    }

    @Override
    public boolean deleteBatch(List<Long> ids,Long memberId) {
        boolean result = false;
        try {
            if (ListUtil.isNotEmpty(ids)) {
                Example example = new Example(MemberAddress.class);
                Example.Criteria criteria = example.createCriteria();
                criteria.andIn("id", ids);
                criteria.andEqualTo("memberid",memberId);
                result = this.memberAddressMapper.deleteByExample(example) > 0;
            }
        } catch (Exception e) {
            this.logger.error("deleteBatch 异常", e);
        }
        return result;
    }

    @Override
    public MemberAddress getOne(Long id) {
        return this.memberAddressMapper.selectByPrimaryKey(id);
    }

    @Override
    public MsgDatasVO getList(Long memberId) {
        MsgDatasVO result = new MsgDatasVO();
        try {
            Example example = new Example(MemberAddress.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("memberid", memberId);
            List<MemberAddress> dataList = this.memberAddressMapper.selectByExample(example);
            result.setTotal(dataList.size());
            result.setDatas(dataList);

        } catch (Exception e) {
            this.logger.error("getList 异常", e);
        }
        return result;
    }

    private void  updateIsDefault(Long memberId,Integer isDefault)
    {
        Example example = new Example(MemberAddress.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("memberid",memberId);
        MemberAddress model=new MemberAddress();
        model.setIsdefault(isDefault);
        this.memberAddressMapper.updateByExampleSelective(model,example);
    }
}
